package com.example.x6.configurationmaintenance.Service;

import android.app.ActivityManager;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.ActivityChooserView;
import android.util.Log;
import com.example.x6.configurationmaintenance.DataBase.ServiceDaemon.ServiceDaemonDatabaseOperation;
import com.example.x6.configurationmaintenance.Interface.ServiceDaemonDatabase.OnServiceDaemonDatabaseChangeListener;
import com.example.x6.configurationmaintenance.Utils.SuCommand;
import java.util.ArrayList;

public class ServiceDaemonService extends IntentService {
    private boolean DEBUG = false;
    private final int INVALID = -1;
    private final int ISRUNNING = 1;
    private final int NOTRUNNING = 0;
    private final String TAG = getClass().getSimpleName();
    private ServiceDaemonDatabaseOperation serviceDaemonDatabaseOperation;
    private String[] services;
    private SuCommand suCommand;

    public ServiceDaemonService() {
        super("ServiceDaemonService");
    }

    /* access modifiers changed from: protected */
    public void onHandleIntent(@Nullable Intent intent) {
        ServiceDaemonDatabaseOperation.initializeInstance(this);
        this.serviceDaemonDatabaseOperation = ServiceDaemonDatabaseOperation.getInstance();
        this.suCommand = new SuCommand();
        this.serviceDaemonDatabaseOperation.setOnServiceDaemonDatabaseChangeListener(new OnServiceDaemonDatabaseChangeListener() {
            /* class com.example.x6.configurationmaintenance.Service.ServiceDaemonService.AnonymousClass1 */

            @Override // com.example.x6.configurationmaintenance.Interface.ServiceDaemonDatabase.OnServiceDaemonDatabaseChangeListener
            public void onServiceDaemonDataBaseChange(String[] serviceList) {
                ServiceDaemonService.this.services = serviceList;
            }
        });
        while (true) {
            mDelay(2000);
            if (this.DEBUG) {
                Log.w(this.TAG, "守护服务正在运行");
            }
            if (this.services != null && this.services.length != 0) {
                String[] strArr = this.services;
                for (String service : strArr) {
                    String[] temp = service.split("/");
                    String packageName = null;
                    String sourceServiceName = null;
                    String serviceName = null;
                    String checkService = null;
                    if (temp.length == 2) {
                        packageName = temp[0];
                        sourceServiceName = temp[1];
                    }
                    if (!(packageName == null || sourceServiceName == null)) {
                        serviceName = sourceServiceName.substring(packageName.length(), sourceServiceName.length()).trim();
                        checkService = packageName + "/" + serviceName;
                    }
                    if (checkService != null) {
                        switch (isServiceRunning(this, packageName + serviceName)) {
                            case -1:
                                if (this.DEBUG) {
                                    Log.e(this.TAG, "无效的服务名");
                                    break;
                                } else {
                                    break;
                                }
                            case 0:
                                if (this.DEBUG) {
                                    Log.e(this.TAG, "服务：" + packageName + serviceName + " 未运行，现在启动");
                                }
                                this.suCommand.execRootCmdSilent("am startservice -n " + service);
                                break;
                            case 1:
                                if (this.DEBUG) {
                                    Log.e(this.TAG, packageName + serviceName + "正在运行...");
                                    break;
                                } else {
                                    break;
                                }
                            default:
                                if (this.DEBUG) {
                                    Log.e(this.TAG, "守护服务正在守护");
                                    break;
                                } else {
                                    break;
                                }
                        }
                    }
                }
            } else if (this.DEBUG) {
                Log.w(this.TAG, "没有需要守护的服务");
            }
        }
    }

    private void startService(String packageName, String serviceName) {
        if (packageName != null && serviceName != null) {
            Intent intent = new Intent();
            intent.setClassName(packageName, packageName + serviceName);
            startService(intent);
        }
    }

    private int isServiceRunning(Context context, String serviceName) {
        if (serviceName.equals("") || serviceName == null) {
            return -1;
        }
        final int MAX_ACTIVITY_COUNT_UNLIMITED = 2147483647;
        ArrayList<ActivityManager.RunningServiceInfo> runningServiceInfos = (ArrayList) ((ActivityManager) context.getSystemService("activity")).getRunningServices(MAX_ACTIVITY_COUNT_UNLIMITED);
        if (runningServiceInfos.size() <= 0) {
            return -1;
        }
        for (int i = 0; i < runningServiceInfos.size(); i++) {
            if (runningServiceInfos.get(i).service.getClassName().equals(serviceName)) {
                return 1;
            }
        }
        return 0;
    }

    private void mDelay(long mill) {
        try {
            Thread.sleep(mill);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
